home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / game / wins1726.zip / PLOT3D.C < prev    next >
C/C++ Source or Header  |  1992-05-14  |  11KB  |  454 lines

  1. /*
  2.     This file includes miscellaneous plot functions and logic
  3.     for 3D, used by lorenz.c and line3d.c
  4.     By Tim Wegner and Marc Reinig.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include "fractint.h"
  9. #include "fractype.h"
  10.  
  11. /* Use these palette indices for red/blue - same on ega/vga */
  12. #define PAL_BLUE    1
  13. #define PAL_RED    2
  14. #define PAL_MAGENTA    3
  15.  
  16. extern int _fastcall targa_color(int x,int y,int color);
  17. extern int Targa_Out, sxoffs, syoffs;
  18.  
  19. int whichimage;
  20. extern int fractype;
  21. extern int mapset;
  22. extern int xadjust;
  23. extern int yadjust;
  24. extern int xxadjust;
  25. extern int yyadjust;
  26. extern int xshift;
  27. extern int yshift;
  28. extern char MAP_name[];
  29. extern int init3d[];
  30. extern int xdots;
  31. extern int ydots;
  32. extern int colors;
  33. extern unsigned char dacbox[256][3];
  34. extern int debugflag;
  35.  
  36. extern int max_colors;
  37.  
  38. int xxadjust1;
  39. int yyadjust1;
  40. int eyeseparation = 0;
  41. int glassestype = 0;
  42. int xshift1;
  43. int yshift1;
  44. int xtrans = 0;
  45. int ytrans = 0;
  46. int red_local_left;
  47. int red_local_right;
  48. int blue_local_left;
  49. int blue_local_right;
  50. int red_crop_left    = 4;
  51. int red_crop_right    = 0;
  52. int blue_crop_left    = 0;
  53. int blue_crop_right = 4;
  54. int red_bright        = 80;
  55. int blue_bright        = 100;
  56.   
  57. unsigned char T_RED;
  58.  
  59. /* Bresenham's algorithm for drawing line */
  60. void _fastcall draw_line (int X1, int Y1, int X2, int Y2, int color)
  61.  
  62. {                /* uses Bresenham algorithm to draw a line */
  63.     int dX, dY;                        /* vector components */
  64.     int row, col,
  65.         final,                        /* final row or column number */
  66.         G,                    /* used to test for new row or column */
  67.         inc1,            /* G increment when row or column doesn't change */
  68.         inc2;                /* G increment when row or column changes */
  69.     char pos_slope;
  70.     extern int xdots,ydots;
  71.  
  72.     dX = X2 - X1;                    /* find vector components */
  73.     dY = Y2 - Y1;
  74.     pos_slope = (dX > 0);                    /* is slope positive? */
  75.     if (dY < 0)
  76.     pos_slope = !pos_slope;
  77.     if (abs (dX) > abs (dY))                /* shallow line case */
  78.     {
  79.         if (dX > 0)            /* determine start point and last column */
  80.         {
  81.             col = X1;
  82.             row = Y1;
  83.             final = X2;
  84.         }
  85.         else
  86.         {
  87.             col = X2;
  88.             row = Y2;
  89.             final = X1;
  90.         }
  91.         inc1 = 2 * abs (dY);            /* determine increments and initial G */
  92.         G = inc1 - abs (dX);
  93.         inc2 = 2 * (abs (dY) - abs (dX));
  94.         if (pos_slope)
  95.             while (col <= final)    /* step through columns checking for new row */
  96.             {
  97.                 (*plot) (col, row, color);
  98.                 col++;
  99.                 if (G >= 0)                /* it's time to change rows */
  100.                 {
  101.                     row++;        /* positive slope so increment through the rows */
  102.                     G += inc2;
  103.                 }
  104.                 else                        /* stay at the same row */
  105.                     G += inc1;
  106.             }
  107.         else
  108.             while (col <= final)    /* step through columns checking for new row */
  109.             {
  110.                 (*plot) (col, row, color);
  111.                 col++;
  112.                 if (G > 0)                /* it's time to change rows */
  113.                 {
  114.                     row--;        /* negative slope so decrement through the rows */
  115.                     G += inc2;
  116.                 }
  117.                 else                        /* stay at the same row */
  118.                     G += inc1;
  119.             }
  120.     }    /* if |dX| > |dY| */
  121.     else                            /* steep line case */
  122.     {
  123.         if (dY > 0)                /* determine start point and last row */
  124.         {
  125.             col = X1;
  126.             row = Y1;
  127.             final = Y2;
  128.         }
  129.         else
  130.         {
  131.             col = X2;
  132.             row = Y2;
  133.             final = Y1;
  134.         }
  135.         inc1 = 2 * abs (dX);            /* determine increments and initial G */
  136.         G = inc1 - abs (dY);
  137.         inc2 = 2 * (abs (dX) - abs (dY));
  138.         if (pos_slope)
  139.             while (row <= final)    /* step through rows checking for new column */
  140.             {
  141.                 (*plot) (col, row, color);
  142.                 row++;
  143.                 if (G >= 0)                    /* it's time to change columns */
  144.                 {
  145.                     col++;    /* positive slope so increment through the columns */
  146.                     G += inc2;
  147.                 }
  148.                 else                    /* stay at the same column */
  149.                     G += inc1;
  150.             }
  151.         else
  152.             while (row <= final)    /* step through rows checking for new column */
  153.             {
  154.                 (*plot) (col, row, color);
  155.                 row++;
  156.                 if (G > 0)                    /* it's time to change columns */
  157.                 {
  158.                     col--;    /* negative slope so decrement through the columns */
  159.                     G += inc2;
  160.                 }
  161.                 else                    /* stay at the same column */
  162.                     G += inc1;
  163.             }
  164.     }
  165. }    /* draw_line */
  166.  
  167.  
  168. /* use this for continuous colors later */
  169. void _fastcall plot3dsuperimpose16b(int x,int y,int color)
  170. {
  171.     int tmp;
  172.     if (color != 0)            /* Keeps index 0 still 0 */
  173.     {
  174.         color = colors - color; /*    Reverses color order */
  175.         color = color / 4;
  176.         if(color == 0)
  177.             color = 1;
  178.     }
  179.     color = 3;
  180.     tmp = getcolor(x,y);
  181.  
  182.     /* map to 4 colors */
  183.     if(whichimage == 1) /* RED */
  184.     {
  185.         if(red_local_left < x && x < red_local_right)
  186.         {
  187.             putcolor(x,y,color|tmp);
  188.             if (Targa_Out)                                
  189.                 targa_color(x, y, color|tmp);
  190.         }
  191.     }
  192.     else if(whichimage == 2) /* BLUE */
  193.         if(blue_local_left < x && x < blue_local_right)
  194.         {
  195.             color = color <<2;
  196.             putcolor(x,y,color|tmp);
  197.             if (Targa_Out)                                
  198.                 targa_color(x, y, color|tmp);
  199.         }
  200. }
  201.  
  202. void _fastcall plot3dsuperimpose16(int x,int y,int color)
  203. {
  204.     int tmp;
  205.  
  206.     tmp = getcolor(x,y);
  207.  
  208.     if(whichimage == 1) /* RED */
  209.     {
  210.         color = PAL_RED;
  211.         if(tmp > 0 && tmp != color)
  212.             color = PAL_MAGENTA;
  213.         if(red_local_left < x && x < red_local_right)
  214.         {
  215.             putcolor(x,y,color);
  216.             if (Targa_Out)                                
  217.                 targa_color(x, y, color);
  218.         }
  219.     }
  220.     else if(whichimage == 2) /* BLUE */
  221.         if(blue_local_left < x && x < blue_local_right)
  222.         {
  223.             color = PAL_BLUE;
  224.             if(tmp > 0 && tmp != color)
  225.                 color = PAL_MAGENTA;
  226.             putcolor(x,y,color);
  227.             if (Targa_Out)                                
  228.                 targa_color(x, y, color);
  229.         }
  230. }
  231.  
  232.  
  233. void _fastcall plot3dsuperimpose256(int x,int y,int color)
  234. {
  235.     int tmp;
  236.     unsigned char t_c;
  237.  
  238.     t_c = 255-color;
  239.  
  240.     if (color != 0)            /* Keeps index 0 still 0 */
  241.     {
  242.         color = colors - color; /*    Reverses color order */
  243.         if (max_colors == 236)
  244.         color = 1 + color / 21; /*    Maps colors 1-255 to 13 even ranges */
  245.         else
  246.         color = 1 + color / 18; /*    Maps colors 1-255 to 15 even ranges */
  247.     }
  248.  
  249.     tmp = getcolor(x,y);
  250.     /* map to 16 colors */
  251.     if(whichimage == 1) /* RED */
  252.     {
  253.         if(red_local_left < x && x < red_local_right)
  254.         {
  255.             /* Overwrite prev Red don't mess w/blue */
  256.             putcolor(x,y,color|(tmp&240));
  257.             if (Targa_Out)
  258.                 if (!ILLUMINE)
  259.                     targa_color(x, y, color|(tmp&240));
  260.                 else
  261.                     targa_writedisk (x+sxoffs, y+syoffs, t_c, 0, 0);
  262.         }
  263.     }
  264.     else if(whichimage == 2) /* BLUE */
  265.         if(blue_local_left < x && x < blue_local_right)
  266.         {
  267.             /* Overwrite previous blue, don't mess with existing red */
  268.             color = color <<4;
  269.             putcolor(x,y,color|(tmp&15));
  270.             if (Targa_Out)                                
  271.                 if (!ILLUMINE)
  272.                     targa_color(x, y, color|(tmp&15));
  273.                 else
  274.                 {
  275.                     targa_readdisk (x+sxoffs, y+syoffs, &T_RED, &tmp, &tmp);
  276.                     targa_writedisk (x+sxoffs, y+syoffs, T_RED, 0, t_c);
  277.                 }
  278.         }
  279. }
  280.  
  281. void _fastcall plotIFS3dsuperimpose256(int x,int y,int color)
  282. {
  283.     int tmp;
  284.     unsigned char t_c;
  285.  
  286.     t_c = 255-color;
  287.  
  288.     if (color != 0)            /* Keeps index 0 still 0 */
  289.     {
  290.         /* my mind is fried - lower indices = darker colors is EASIER! */
  291.         color = colors - color; /*    Reverses color order */
  292.         if (max_colors == 236)
  293.         color = 1 + color / 21; /*    Maps colors 1-255 to 13 even ranges */
  294.         else
  295.         color = 1 + color / 18; /*    Looks weird but maps colors 1-255 to 15
  296.                     relatively even ranges */
  297.     }
  298.  
  299.     tmp = getcolor(x,y);
  300.     /* map to 16 colors */
  301.     if(whichimage == 1) /* RED */
  302.     {
  303.         if(red_local_left < x && x < red_local_right)
  304.         {
  305.             putcolor(x,y,color|tmp);
  306.             if (Targa_Out)                                
  307.                 if (!ILLUMINE)
  308.                     targa_color(x, y, color|tmp);
  309.                 else
  310.                     targa_writedisk (x+sxoffs, y+syoffs, t_c, 0, 0);
  311.         }
  312.     }
  313.     else if(whichimage == 2) /* BLUE */
  314.         if(blue_local_left < x && x < blue_local_right)
  315.         {
  316.             color = color <<4;
  317.             putcolor(x,y,color|tmp);
  318.             if (Targa_Out)                                
  319.                 if (!ILLUMINE)
  320.                     targa_color(x, y, color|tmp);
  321.                 else
  322.                 {
  323.                     targa_readdisk (x+sxoffs, y+syoffs, &T_RED, &tmp, &tmp);
  324.                     targa_writedisk (x+sxoffs, y+syoffs, T_RED, 0, t_c);
  325.                 }
  326.         }
  327. }
  328.  
  329. void _fastcall plot3dalternate(int x,int y,int color)
  330. {
  331.     int tmp;
  332.     unsigned char t_c;
  333.  
  334.     t_c = 255-color;
  335.     /* lorez high color red/blue 3D plot function */
  336.     /* if which image = 1, compresses color to lower 128 colors */
  337.  
  338.     /* my mind is STILL fried - lower indices = darker colors is EASIER! */
  339.     color = colors - color;
  340.     if((whichimage == 1) && !((x+y)&1)) /* - lower half palette */
  341.     {
  342.         if(red_local_left < x && x < red_local_right)
  343.         {
  344.             putcolor(x,y,color>>1);
  345.             if (Targa_Out)                                
  346.                 if (!ILLUMINE)
  347.                     targa_color(x, y, color>>1);
  348.                 else
  349.                     targa_writedisk (x+sxoffs, y+syoffs, t_c, &tmp, &tmp);
  350.         }
  351.     }
  352.     else if((whichimage == 2) && ((x+y)&1) ) /* - upper half palette */
  353.     {
  354.         if(blue_local_left < x && x < blue_local_right)
  355.         {
  356.             putcolor(x,y,(color>>1)+(colors>>1));
  357.             if (Targa_Out)                                
  358.                 if (!ILLUMINE)
  359.                     targa_color(x, y, (color>>1)+(colors>>1));
  360.                 else
  361.                     targa_writedisk (x+sxoffs, y+syoffs, T_RED, 0, t_c);
  362.         }
  363.     }
  364. }
  365.  
  366. void plot_setup()
  367. {
  368.     double d_red_bright, d_blue_bright;
  369.     int i;
  370.  
  371.     /* set funny glasses plot function */
  372.     switch(glassestype)
  373.     {
  374.     case 1:
  375.         standardplot = plot3dalternate;
  376.         break;
  377.  
  378.     case 2:
  379.         if(colors == 256)
  380.             if (fractype != IFS3D)
  381.             {
  382.                 standardplot = plot3dsuperimpose256;
  383.             }
  384.             else
  385.                 standardplot = plotIFS3dsuperimpose256;
  386.         else
  387.               standardplot = plot3dsuperimpose16;
  388.         break;
  389.  
  390.     default:
  391.         standardplot = putcolor;
  392.         break;
  393.     }
  394.  
  395.     xshift1 = xshift = (XSHIFT * (double)xdots)/100;
  396.     yshift1 = yshift = (YSHIFT * (double)ydots)/100;
  397.  
  398.     if(glassestype)
  399.     {
  400.         red_local_left    =    (red_crop_left        * (double)xdots)/100.0;
  401.         red_local_right    =    ((100 - red_crop_right)    * (double)xdots)/100.0;
  402.         blue_local_left    =    (blue_crop_left        * (double)xdots)/100.0;
  403.         blue_local_right =    ((100 - blue_crop_right) * (double)xdots)/100.0;
  404.         d_red_bright    =    (double)red_bright/100.0;
  405.         d_blue_bright    =    (double)blue_bright/100.0;
  406.  
  407.         switch(whichimage)
  408.         {
  409.         case 1:
  410.             xshift    += (eyeseparation* (double)xdots)/200;
  411.             xxadjust = ((xtrans+xadjust)* (double)xdots)/100;
  412.             xshift1    -= (eyeseparation* (double)xdots)/200;
  413.             xxadjust1 = ((xtrans-xadjust)* (double)xdots)/100;
  414.             break;
  415.  
  416.         case 2:
  417.             xshift    -= (eyeseparation* (double)xdots)/200;
  418.             xxadjust = ((xtrans-xadjust)* (double)xdots)/100;
  419.             break;
  420.         }
  421.     }
  422.     else
  423.         xxadjust = (xtrans* (double)xdots)/100;
  424.         yyadjust = -(ytrans* (double)ydots)/100;
  425.  
  426.     if (mapset)
  427.     {
  428.         ValidateLuts(MAP_name);    /* read the palette file */
  429.         if(glassestype==1 || glassestype==2)
  430.         {
  431.             if(glassestype == 2 && colors < 256)
  432.             {
  433.                 dacbox[PAL_RED    ][0] = 63;
  434.                 dacbox[PAL_RED    ][1] =    0;
  435.                 dacbox[PAL_RED    ][2] =    0;
  436.  
  437.                 dacbox[PAL_BLUE    ][0] =    0;
  438.                 dacbox[PAL_BLUE    ][1] =    0;
  439.                 dacbox[PAL_BLUE    ][2] = 63;
  440.  
  441.                 dacbox[PAL_MAGENTA][0] = 63;
  442.                 dacbox[PAL_MAGENTA][1] =    0;
  443.                 dacbox[PAL_MAGENTA][2] = 63;
  444.             }
  445.             for (i=0;i<256;i++)
  446.             {
  447.                 dacbox[i][0] = dacbox[i][0] * d_red_bright;
  448.                 dacbox[i][2] = dacbox[i][2] * d_blue_bright;
  449.             }
  450.         }
  451.         spindac(0,1); /* load it, but don't spin */
  452.     }
  453. }
  454.